﻿using HandyControl.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WpfTool.Models;
using Window = System.Windows.Window;

namespace WpfTool.XiTong
{
    /// <summary>
    /// Menu.xaml 的交互逻辑
    /// </summary>
    public partial class Menu : UserControl
    {
        public Menu()
        {
            InitializeComponent();
            Init();
        }

        public void Init()
        {
            List<WpfMenu> list = DbHelper.DbMssql.Queryable<WpfMenu>().ToList();
            DgList.ItemsSource = list;
        }

        private void BtnAdd_Click(object sender, RoutedEventArgs e)
        {
            CreateWindow("添加菜单", Guid.Empty);
        }

        private void PopupClosed(object? sender, EventArgs e)
        {
            Init();
        }

        private void BtnEdit_Click(object sender, RoutedEventArgs e)
        {
            Button button = (Button)sender;
            WpfMenu current = (WpfMenu)button.DataContext;
            CreateWindow("编辑菜单", current.Id);
        }

        private void CreateWindow(string title, Guid Id)
        {
            Save save = new Save();
            save.Id = Id;

            PopupWindow pWin = new PopupWindow();
            pWin.PopupElement = save;
            pWin.Title = title;
            pWin.ShowTitle = true;
            pWin.ShowBorder = false;
            pWin.BorderThickness = new Thickness(0);
            pWin.Background = new SolidColorBrush(Colors.LightBlue);
            pWin.Padding = new Thickness(50);
            pWin.Closed += PopupClosed;
            pWin.Show();

            var parent = Window.GetWindow(this);
            pWin.Left = parent.Left + (parent.Width - pWin.Width) / 2;
            pWin.Top = parent.Top + (parent.Height - pWin.Height) / 2;
        }

        private void BtnDelete_Click(object sender, RoutedEventArgs e)
        {
            Button button = (Button)sender;
            WpfMenu current = (WpfMenu)button.DataContext;

            DbHelper.DbMssql.Deleteable(current).ExecuteCommand();
            Init();
        }
    }
}
